package com.chushouya.controller.manager.api;

import com.chushouya.common.annotations.AuthValidate;
import com.chushouya.common.constant.ServiceType;
import com.chushouya.common.support.Contexts;
import com.general.framework.core.schema.Response;
import com.chushouya.manager.dto.admin.user.UserCenterDTO;
import com.chushouya.manager.dto.admin.user.password.SetPasswordDTO;
import com.chushouya.manager.dto.admin.user.password.UpdatePasswordDTO;
import com.chushouya.manager.service.api.UserCenterApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 用户中心接口控制器
 * 提供用户中心相关数据查询功能
 * 
 * @author chushouya
 */
@Api(tags = ServiceType.API + "用户中心接口数据")
@RestController
@RequestMapping(ServiceType.API + "/userCenter")
@Slf4j
public class UserCenterController {
    
    @Resource
    private UserCenterApiService userCenterApiService;

    @AuthValidate
    @ApiOperation("获取用户中心数据")
    @GetMapping("/user")
    public Response<UserCenterDTO> userCenter() {
        final Long userId = Contexts.getUserContext().getUserId();
        UserCenterDTO userCenterData = userCenterApiService.getUserCenter(userId);
        return Response.success(userCenterData);
    }

    @AuthValidate
    @ApiOperation("设置密码")
    @PostMapping("/setPassword")
    public Response<Void> setPassword(@RequestBody @Valid SetPasswordDTO dto) {
        final Long userId = Contexts.getUserContext().getUserId();
        userCenterApiService.setPassword(userId, dto);
        return Response.success();
    }

    @AuthValidate
    @ApiOperation("修改密码")
    @PostMapping("/updatePassword")
    public Response<Void> updatePassword(@RequestBody @Valid UpdatePasswordDTO dto) {
        final Long userId = Contexts.getUserContext().getUserId();
        userCenterApiService.updatePassword(userId, dto);
        return Response.success();
    }

    @AuthValidate
    @ApiOperation("注销账号")
    @PostMapping("/deleteAccount")
    public Response<Void> deleteAccount() {
        final Long userId = Contexts.getUserContext().getUserId();
        userCenterApiService.deleteAccount(userId);
        return Response.success();
    }
}
